<%-- 
    Document   : index
    Created on : 02.11.2011, 17:32:29
    Author     : Дима
--%>

<%@page import="java.io.IOException"%>
<%@page import="org.jdom.Element"%>
<%@page import="ru.ssau.lib.StringIntegerPair"%>
<%@page import="ru.ssau.utils.XMLHelper"%>
<%@page import="ru.ssau.server.Constants"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page errorPage="error.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>   <%!
    private StringIntegerPair[] getStringIntegerPairList(HttpSession session, String parameter) throws java.io.IOException {
        
        String userListXMLString = (String) session.getAttribute(parameter);
        if (userListXMLString != null) {
            Element userListElement = XMLHelper.parseString(userListXMLString);
            StringIntegerPair[] values = 
                    XMLHelper.loadStringIntegerPairs(userListElement.getChild(Constants.VALUES_XML_TAG));
            return values;
        } else {
            return new StringIntegerPair[0];
        }
    } %>
    <head>
        <script type="text/javascript">
            function loadUser() {
                var req = new XMLHttpRequest();
                req.onreadystatechange = function() {
                    if(req.status == 200) {
                        var user = req.responseXML.documentElement;
                        var milliseconds = parseInt(user.getElementsByTagName("registrationDate")[0].firstChild.nodeValue, 10);
                        document.getElementById("regDate").innerHTML = new Date(milliseconds);
                        document.getElementById("login").innerHTML = user.getElementsByTagName("login")[0].firstChild.nodeValue;
                        document.getElementById("password").innerHTML = user.getElementsByTagName("password")[0].firstChild.nodeValue;
                        document.getElementById("info").innerHTML = user.getElementsByTagName("info")[0].firstChild.nodeValue;
                    } else {
                        //TODO
                        //document.getElementById("userInfo").innerHTML="error";
                    }
                }
                req.overrideMimeType('text/xml');
                var userId = document.getElementById("userList").value;
                req.open("GET", "request?action=getusr&id=" + userId, true);
                req.send();
            }
        </script>
        <script type="text/javascript">
            function loadThread() {
                var req = new XMLHttpRequest();
                req.onreadystatechange = function() {
                if(req.status == 200) {
                        var thread = req.responseXML.documentElement;
                        
                        document.getElementById("threadId").innerHTML = thread.getElementsByTagName("id")[0].firstChild.nodeValue;
                        document.getElementById("threadCaption").innerHTML = thread.getElementsByTagName("caption")[0].firstChild.nodeValue;
                    } else {
                        //TODO
                        //document.getElementById("threadCaption").innerHTML="error";
                    }
                }
                req.overrideMimeType('text/xml');
                var threadId = document.getElementById("threadList").value;
                req.open("GET", "request?action=getthr&id=" + threadId, true);
                req.send();
            }
        </script>
        <script type="text/javascript">
            function loadMessage() {
                var req = new XMLHttpRequest();
                req.onreadystatechange = function() {
                    if(req.status == 200) {
                        var message = req.responseXML.documentElement;
                        var milliseconds = parseInt(message.getElementsByTagName("postDate")[0].firstChild.nodeValue, 10);
                        document.getElementById("postDate").innerHTML = new Date(milliseconds);
                        document.getElementById("text").innerHTML = message.getElementsByTagName("text")[0].firstChild.nodeValue;
                        document.getElementById("userIdInMessage").innerHTML = message.getElementsByTagName("userId")[0].firstChild.nodeValue;
                        document.getElementById("threadIdInMessage").innerHTML = message.getElementsByTagName("threadId")[0].firstChild.nodeValue;
                    } else {
                        //TODO
                        //document.getElementById("text").innerHTML="error";
                    }
                }
                req.overrideMimeType('text/xml');
                var messageId = document.getElementById("messageList").value;
                req.open("GET", "request?action=getmes&id=" + messageId, true);
                req.send();
            }
        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Forum manager</title>
    </head>
    <body onload="loadUser(); loadThread(); loadMessage()">
        <a href="<%=String.format("request?%s=%s", Constants.ACTION, Constants.GET_MODEL_STATE)%>">
            refresh this page
        </a>
        <br>
        <h3>User section</h3>
        <form name="searchUserForm"  action="request">
            Search user by <i>name</i> attribute:
            <table>
                <tr>
                    <td>
                        <input type="text" size="20" name="<%=Constants.USER_SEARCH_PARAMETER%>"/>
                    </td>
                    <td>
                        &nbsp
                        <button name="<%=Constants.ACTION%>" value="<%=Constants.SEARCH_USER%>">Submit</button>
                    </td>
                </tr>
            </table>
        </form>
        <form name="userMainForm" action="request">
            <table cellspacing="10">
                <tr>
                    <td>   <%
                      StringIntegerPair[] users;
                      users = getStringIntegerPairList(session, Constants.USER_LIST);
                      if(users != null) { %>
                        <select id="userList" onchange="loadUser()" name="<%=Constants.ID%>">   <%
                          for(int i = 0; i < users.length; i++) { %>
                            <option <%=(i==0)?"selected":""%> value="<%=users[i].getNumber()%>"> <%=users[i]%> </option>   <%
                          } %>
                        </select>   <%
                      } %>
                    </td>
                    <td>
                        <button name="<%=Constants.ACTION%>" value="<%=Constants.ADD_USER%>">Add user</button>
                    </td>
                    <td>
                        <button name="<%=Constants.ACTION%>" value="<%=Constants.EDIT_USER%>">Edit user</button>
                    </td>
                    <td>
                        <button name="<%=Constants.ACTION%>" value="<%=Constants.DELETE_USER%>">Delete user</button>
                    </td>
                </tr>
            </table>
            <b>Reg. date: </b>
            <span id="regDate"></span>
            <br>
            <b>Login: </b>
            <span id="login"></span>&nbsp&nbsp&nbsp&nbsp
            <b>Password: </b>
            <span id="password"></span>
            <br>
            <b>Info: </b>
            <span id="info"></span>
        </form>
        
        <hr>

        <h3>Thread section</h3>
        <form name="searchThreadForm"  action="request">
            Search thread by <i>caption</i> attribute:
            <table>
                <tr>
                    <td>
                        <input type="text" size="20" name="<%=Constants.THREAD_SEARCH_PARAMETER%>"/>
                    </td>
                    <td>
                        &nbsp
                        <button name="<%=Constants.ACTION%>" value="<%=Constants.SEARCH_THREAD%>">Submit</button>
                    </td>
                </tr>
            </table>
        </form>
        <form name="threadMainForm" action="request">
            <table cellspacing="10">
                <tr>
                    <td>   <%
                      StringIntegerPair[] threads;
                      threads = getStringIntegerPairList(session, Constants.THREAD_LIST);
                      if(threads != null) { %>
                        <select id="threadList" onchange="loadThread()" name="<%=Constants.ID%>">   <%
                          for(int i = 0; i < threads.length; i++) { %>
                            <option <%=(i==0)?"selected":""%> value="<%=threads[i].getNumber()%>"> <%=threads[i]%> </option>   <%
                          } %>
                        </select>   <%
                      } %>
                    </td>
                    <td>
                        <button name="<%=Constants.ACTION%>" value="<%=Constants.ADD_THREAD%>">Add thread</button>
                    </td>
                    <td>
                        <button name="<%=Constants.ACTION%>" value="<%=Constants.EDIT_THREAD%>">Edit thread</button>
                    </td>
                    <td>
                        <button name="<%=Constants.ACTION%>" value="<%=Constants.DELETE_THREAD%>">Delete thread</button>
                    </td>
                </tr>
            </table>
            <br>
            <b>Id: </b>
            <span id="threadId"></span>
            <br>
            <b>Caption: </b>
            <span id="threadCaption"></span>
        </form>
        
        <hr>

        <h3>Message section</h3>
        <form name="searchMessageForm"  action="request">
            Search message by <i>text</i> attribute:
            <table>
                <tr>
                    <td>
                        <input type="text" size="20" name="<%=Constants.MESSAGE_SEARCH_PARAMETER%>"/>
                    </td>
                    <td>
                        &nbsp
                        <button name="<%=Constants.ACTION%>" value="<%=Constants.SEARCH_MESSAGE%>">Submit</button>
                    </td>
                </tr>
            </table>
        </form>
        <form name="messageMainForm" action="request">
            <table cellspacing="10">
                <tr>
                    <td>   <%
                      StringIntegerPair[] messages;
                      messages = getStringIntegerPairList(session, Constants.MESSAGE_LIST);
                      if(messages != null) { %>
                        <select id="messageList" onchange="loadMessage()" name="<%=Constants.ID%>">   <%
                          for(int i = 0; i < messages.length; i++) { %>
                            <option <%=(i==0)?"selected":""%> value="<%=messages[i].getNumber()%>"> <%=messages[i]%> </option>   <%
                          } %>
                        </select>   <%
                      } %>
                    </td>
                    <td>
                        <button name="<%=Constants.ACTION%>" value="<%=Constants.ADD_MESSAGE%>">Add message</button>
                    </td>
                    <td>
                        <button name="<%=Constants.ACTION%>" value="<%=Constants.EDIT_MESSAGE%>">Edit message</button>
                    </td>
                    <td>
                        <button name="<%=Constants.ACTION%>" value="<%=Constants.DELETE_MESSAGE%>">Delete message</button>
                    </td>
                </tr>
            </table>
            <b>Post date: </b>
            <span id="postDate"></span>
            <br>
            <b>User id: </b>
            <span id="userIdInMessage"></span>&nbsp&nbsp&nbsp&nbsp
            <b>Thread id: </b>
            <span id="threadIdInMessage"></span>
            <br>
            <b>Text </b>
            <span id="text"></span>
        </form>
    </body>
</html>
